package com.zhu.transaction.controller;


import com.zhu.transaction.pojo.*;
import com.zhu.transaction.pojo.state.BackState;
import com.zhu.transaction.pojo.state.R;
import com.zhu.transaction.service.*;
import com.zhu.transaction.service.imp.CategoryServiceImp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhu
 * @since 2021-01-07
 */
@Controller
@RequestMapping("/admin")
@Api(tags = "管理员接口", description = "后台操作系统")
public class AdminController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    UserService userService;

    @Autowired
    ProductService productService;


    @Autowired
    OrderService orderService;

    @Autowired
    MessageService messageService;

    @ResponseBody
    @PostMapping("/category/add")
    @ApiOperation(value = "添加分类")
    public R addCategory(String firstCategory, String[] secondCategory) {
        int result = categoryService.addCategory(firstCategory, secondCategory);
        return result > 0 ? R.ok().message("添加成功").data("data", categoryService.getAllCategory())
                : R.error().message("添加失败");
    }

    @ResponseBody
    @GetMapping("/category/all")
    @ApiOperation(value = "获取所有分类信息")
    public R getAllCategoryInfo() {
        List<BackCategory> allCategory = categoryService.getAllCategory();
        return R.ok().data("data", allCategory);
    }

    @ResponseBody
    @PostMapping("/category/delete")
    @ApiOperation(value = "删除分类信息")
    public R deleteCategory(@RequestParam @ApiParam(value = "分类ID值", required = true) String allID) {
        int result = categoryService.deleteCategory(allID);
        return result > 0 ? R.ok().message("已删除").data("data", categoryService.getAllCategory())
                : R.error().message("删除失败");
    }

    @ResponseBody
    @PostMapping("/category/update")
    @ApiOperation(value = "更新分类信息")
    public R updateCategory(@RequestParam @ApiParam(value = "分类ID值") String id,
                            @RequestParam @ApiParam(value = "新的分类名") String categoryName) {
        int result = categoryService.updateCategory(id, categoryName);
        String message = "更新失败";
        if (result == -2) message = "该分类名已经存在";
        return result > 0 ? R.ok().message("更新成功").data("data", categoryService.getAllCategory())
                : R.error().message(message);
    }


    @ResponseBody
    @GetMapping("/user/getAllUser")
    @ApiOperation(value = "获取所有用户信息")
    public R getAllUser(Boolean isAdminUser) {
        List<User> allUser = userService.getAdminUser(isAdminUser);
        return R.ok().data("users", allUser).data("count", allUser.size()).code(0);
    }

    @ResponseBody
    @GetMapping("/user/updateAccountType")
    @ApiOperation(value = "更新指定用户的类型")
    public R updateAccountType(String userId, Integer type) {
        BackState backState = userService.updateUserType(userId, type);
        return backState == BackState.SUCCESS ? R.ok().message("操作成功") : R.error().message("操作失败");
    }

    @ResponseBody
    @GetMapping("/user/updateAccountState")
    @ApiOperation(value = "更新指定用户的状态")
    public R updateAccountState(String userId, Integer type) {
        BackState backState = userService.updateUserState(userId, type);
        return backState == BackState.SUCCESS ? R.ok().message("操作成功") : R.error().message("操作失败");
    }

    @ResponseBody
    @GetMapping("product/deleteProduct")
    @ApiOperation(value = "删除违规商品")
    public R deleteProduct(String productId) {
        BackState backState = productService.deleteProduct(productId, true);
        return backState == BackState.SUCCESS ? R.ok().message("操作成功") : R.error().message("操作失败");
    }


    @ResponseBody
    @GetMapping("/product/getAllProduct")
    @ApiOperation(value = "获取所有未出售的商品信息")
    public R getAllProduct() {
        List<Product> allProduct = productService.getAllProduct("0");
        for (Product product : allProduct) {
            product.setMyCategory(categoryService.getCategoryById(product.getCategory()));
            product.setReleaseUserPojo(userService.getOneUserById(product.getReleaseUser()));
        }
        return R.ok().data("products", allProduct).data("count", allProduct.size()).code(0);
    }


    @ResponseBody
    @GetMapping("/order/getAllOrder")
    @ApiOperation(value = "获取所有订单信息")
    public R getAllOrder() {
        List<OrderItem> allOrderList = orderService.getAllOrderList();
        return R.ok().data("orders", allOrderList).data("count", allOrderList.size()).code(0);
    }

    @ResponseBody
    @GetMapping("/message/getAllMessage")
    @ApiOperation(value = "获取所有消息信息")
    public R getAllMessage() {
        List<Message> messageList = messageService.getMessageByReceiveUser("0");
        return R.ok().data("messages", messageList).data("count", messageList.size()).code(0);
    }

    @ResponseBody
    @PostMapping("/message/update")
    @ApiOperation(value = "修改消息状态")
    public R updateMessageState(String messageId) {
        BackState backState = messageService.updateMessage(messageId);
        return backState == BackState.SUCCESS ? R.ok().message("成功") : R.error();
    }

    @ResponseBody
    @PostMapping("/message/delete")
    @ApiOperation(value = "修改消息状态")
    public R deleteMessageState(String messageId) {
        BackState backState = messageService.deleteOneMessage(messageId);
        return backState == BackState.SUCCESS ? R.ok().message("操作成功") : R.error().message("操作失败");
    }

    @ResponseBody
    @PostMapping("/message/add")
    @ApiOperation(tags = "添加一条留言", value = "")
    public R addMessage(@RequestBody Message message) {
        message.setSendUser("0").setMessageType(3);
        BackState result = messageService.addMessage(message);
        return result == BackState.SUCCESS ? R.ok().message("回复成功") : R.error().message("回复失败");
    }

}

